<?php

namespace App\Http\Controllers\Bend;


use App\Logics\StudentLogic;
use App\Models\User;

class StudentController extends BaseController
{
    // 一般操作
    public function initialize()
    {
        parent::initialize();

        // 操作逻辑
        $this->model = 'Student';

        // 必须是认证用户才能操作
        $user = $this->myUser;
        $opt  = $this->optLogic();
        $opt->setField('myUid', $this->myUid);
        $opt->throwIf(empty($user), '请先登录在操作！', LOGIN_ERROR);
        $opt->throwIf(empty($user['is_auth']), '你没有认证！');
        $opt->throwIf($user['verify'] != 1, '你的用户认证未通过！');
        //$opt->throwIf($user['auth_type'] != 'school', '非学校认证不能操作学员信息！');
    }


    #################################################################################################

    // 学员：认证学校添加学员
    //   只针对当前的学校认证用户才有的操作

    ## 页面展示
    // 列表数据展示
    protected function listTable($lists)
    {
        // 整理
        $trList = [];
        if(empty($lists)){
            return $trList;
        }

        // 获取用户信息
        $uids  = array_column($lists, 'uid');
        $users = (new User())->isCache(true)->getListInIds($uids);

        // 展示列表表格
        $statuses = [1 => '接入', 0 => '未接入', -1 => '删除'];
        $sexes    = [1 => '男', 2 => '女',];  // 0 => '保密'
        $listHtml = $this->adminList();
        foreach($lists as $key => $item){
            // 数据
            $_id      = $item['id'];
            $status   = $item['status'] == 1 ? '接入' : '未接入';
            $statusOp = $item['status'] == 1 ? '禁用' : '启用';
            // 操作
            $operate = '';
            //$operate .= $listHtml->operate_status(['id' => $_id, 'value' => 1 - $item['status']], $statusOp, $item['status']);
            $operate .= $listHtml->operate_edit(['id' => $_id], '编辑学员');
            $operate .= $listHtml->operate_delete(['id' => $_id], '你确定要删除该学员吗？');
            //$operate .= $listHtml->operate_soft_delete(['id' => $_id], '你确定要删除该学员吗？');

            // 页面
            $tr = $listHtml->list_check($item['id']);
            //$tr .= $listHtml->list_td($users[$item['uid']]['nickname'] ?? '-');
            //$tr .= $listHtml->list_td($item['person_uid']);
            $tr .= $listHtml->list_td($item['person_name']);
            $tr .= $listHtml->list_td($sexes[$item['sex']] ?? '-');
            $tr .= $listHtml->list_td($item['age']);
            $tr .= $listHtml->list_td($item['guardor']);
            $tr .= $listHtml->list_td($item['mobile']);
            $tr .= $listHtml->list_td($status);
            $tr .= $listHtml->list_td(date('Y-m-d H:i', $item['ctime']));
            $tr .= $listHtml->list_td($operate);

            // 整合
            $trList[] = $listHtml->list_tr($tr);
        }

        // 整合
        return $trList;
    }

    // 列表顶部按钮
    protected function listTopButton()
    {
        //  <button class="layui-btn layuiadmin-btn-useradmin" data-type="batchdel">删除</button>
        $addUrl     = Url('/bend/student/add');
        $delUrl     = Url('/bend/student/batch-delete');
        $top_button = '<button href="javascript:;" onclick="onEdit(\''.$addUrl.'\', \'添加学员\')" class="layui-btn layuiadmin-btn-useradmin" data-type="add">添加</a> ';
        $top_button .= '<button href="javascript:;" onclick="batchDelete(\''.$delUrl.'\')" class="layui-btn layuiadmin-btn-useradmin" data-type="batchdel">删除</a> ';
        $this->assign('top_button', $top_button);
    }

    // 列表字段
    protected function listFields()
    {
        // 姓名	性别	年龄	监护人	手机号	状态	添加时间	操作
        // 'uid' => '认证用户', 'person_uid' => '接入用户',
        return ['person_name' => '姓名', 'sex' => '性别', 'age' => '年龄', 'guardor' => '监护人', 'mobile' => '手机号', 'status' => '状态', 'ctime' => '添加时间', 'operate' => '操作',];
    }

    // 列表搜索
    protected function listSearch()
    {
        $sexs    = [1 => '男', 2 => '女', '' => '不限'];
        $request = $this->optLogic()->getData();
        $form    = $this->adminList();

        $form_uid    = $form->formInput('form_uid', $request['form_uid'] ?? null, '从用户id', '120px');
        $keyword     = $form->formInput('keyword', $request['keyword'] ?? '', '关键字');
        $sex         = $form->formSelect('sex', $request['sex'] ?? '', '性别', $sexs, '120px');
        $age         = $form->formNumber('age', $request['age'] ?? '', '学员年龄', '120px');
        $person_name = $form->formInput('person_name', $request['person_name'] ?? '', '学员姓名');
        $mobile      = $form->formInput('mobile', $request['mobile'] ?? '', '联系电话');
        $search      = ['person_name' => $person_name, 'age' => $age, 'mobile' => $mobile, 'sex' => $sex,];
        $this->assign('search', $search);

        $searchHide = ['channel'];
        $this->assign('searchHide', []);
    }

    // 表单页面
    protected function formTable()
    {
        $opt = $this->optLogic();
        $opt->setField('student_id', $opt->getData('id'));
        $manage = $this->checkLogic($this->model);
        if($manage === false){
            return $this->error($manage->getError());
        }

        $html = '';
        $info = $manage->onShow($this->myUid);
        $form = $this->adminForm();

        // 用户
        $uInfo = (new User())->isCache(true)->getOneById($info['uid'] ?? 0);

        // 用户名
        //$role     = $form->formCheckbox('role', json_decode($info['role']), $all); // 角色
        //$status   = $form->formRadio('status', $info['status'] ?? 1, [1 => '正常', 0 => '禁用']);
        //$uid         = $form->ajaxSearch('uid', $info['uid'] ?? '', 'User');
        //$uid         = $form->ajaxSingleFromList('uid', $uInfo, 'User', 'id', 'username');
        //$head_img    = $form->formImage('head_img', $info['head_img'] ?? '');
        $person_name = $form->formInput('person_name', $info['person_name'] ?? '', '姓名');
        $sex         = $form->formSelect('sex', $info['sex'] ?? 0, [1 => '男', 2 => '女',]);  // 0 => '保密'
        $age         = $form->formNumber('age', $info['age'] ?? '', '120px');
        $guardor     = $form->formInput('guardor', $info['guardor'] ?? '', '监护人');
        $mobile      = $form->formInput('mobile', $info['mobile'] ?? '', '手机号');
        $_id         = $form->formHidden('id', $info['id'] ?? '');

        //$html .= $form->makeItem('确认密码', $password, '添加时候不填默认密码123456，修改不填默认不修改');
        //$html .= $form->makeItem('认证用户', $uid);
        //$html .= $form->makeItem('头像', $head_img);
        $html .= $form->makeItem('学员姓名', $person_name);
        $html .= $form->makeItem('手机号', $mobile);
        $html .= $form->makeItem('性别', $sex);
        $html .= $form->makeItem('年龄', $age);
        $html .= $form->makeItem('监护人', $guardor);
        $html .= $_id;

        return $html;
    }



    #################################################################################################
    ## 操作
    // 删除学员
    public function onDelete()
    {
        $logic = new StudentLogic();
        //$res   = $logic->onDelete($this->myUid);  // 直接删除（这里用户的学员管理，很少操作，所以直接删除，而且软删除的没有保留的意义）
        $res = $logic->onSoft($this->myUid);  // 软删除
        if($res === false){
            return $this->error($logic->getError());
        }

        return $this->success();
    }

    // 逻辑
    protected function checkLogic($logic = '')
    {
        $opt    = $this->optLogic();
        $action = $opt->getData('action', null, 'trim');
        $status = $opt->getData('status', null, 'intval');
        $opt->setField('myUid', $this->myUid);
        if(is_null($status) && in_array($action, ['lists', 'on-lists'])){
            $opt->setField('status', 999);
        }

        return new StudentLogic();
    }


}


